<?php
class StudentModel extends Model {

    /**
     * StudentModel class constructor.
     */
    function StudentModel()
    {
        parent::Model();

    }

    /**
     * Method for adding a new student.
     * @param array $studentData
     * @param string $username
     */
    function addNewStudent($studentData, $username)
    {
        //Get the primary key from the username.
        $this->db->select('pk');
        $this->db->where('username', $username);
        $subquery = $this->db->get('users');
        $subquery = $subquery->result_array();
        $subquery = $subquery[0];
        $subquery = $subquery['pk'];

        $studentData['user_fk'] = $subquery;
        $this->db->insert('students', $studentData);
    }

    /**
     * Method for deleting a student by the primary key.
     * @param int $pk
     */
    function deleteStudent($pk)
    {
        $this->db->where('pk', $pk);
        $this->db->delete('students');
    }

    /**
     * Method for updating a student record by the primary key.
     * @param array $studentData
     * @param int $pk
     */
    function updateStudent($studentData, $pk)
    {
        $this->db->where('pk', $pk);
        $this->db->update('students', $studentData);
    }

    /**
     * Method for retrieving a student's info by the primary key.
     * It requires a username to verify that the user has control of the
     * student record being retrieved.
     * @param int $pk
     * @param string $username
     * @return array or boolean
     */
    function getStudentInfoByPK($pk, $username)
    {
        $this->db->select('students.pk');
        $this->db->from('students');
        $this->db->join('users', 'students.user_fk = users.pk', 'inner');
        $this->db->where('users.username', $username);
        $this->db->where('students.pk', $pk);
        $query = $this->db->get();

       //Verify that the student who's info is being pulled belongs to the user.
       if($query->num_rows == 0)
                return false;
       else
       {
            $this->db->select('students.user_fk, students.fname, students.lname, students.school, students.dob, students.gradelevel');
            $this->db->where('pk', $pk);
            $query = $this->db->get('students');

            $ret = $query->result_array();

            return $ret[0];
       }

    }

    /**
     * Method for retrieving all atudent info entered by a given user.
     * @param string $username
     * @return array
     */
    function getStudentsInfoByUsername($username)
    {
        $this->db->select('students.pk, students.user_fk, students.fname, students.lname, students.school, students.dob, students.gradelevel');
        $this->db->from('students');
        $this->db->join('users', 'students.user_fk = users.pk', 'inner');
        $this->db->where('users.username', $username);
        $query = $this->db->get();

        $ret = $query->result_array();

        return $ret;
    }

    /**
     * Method for testing to see if a user has added any students.
     * @param string $username
     * @return boolean
     */
    function studentsExist($username)
    {
        $this->db->select('students.pk');
        $this->db->from('students');
        $this->db->join('users', 'students.user_fk = users.pk', 'inner');
        $this->db->where('users.username', $username);
        $query = $this->db->get();

        if($query->num_rows == 0)
                return false;
        else
            return true;
    }

}



?>
